from base.myjson import JSONResponse
from django.views.decorators.csrf import csrf_exempt
from app.model.ActorActivityModel import QyActor_activities
from app.model.MemberModel import QyMember,MemberSerializer
from app.model.ActivityModel import QyActivity,Activity_my,Activity_info
from app.model.GroupModel import QyGroup
from base.my_page import mypage
import json
import time


# Create your views here.



@csrf_exempt
def Activity_my_list(request):
    '''
    获得我参加的活动列表
    :param request:
    :return:
    '''
    if request.method == 'GET':
        try:
            member_id = QyMember.objects.get(openid=request.GET.get('openid')).id
            obj = QyActor_activities.objects.filter(
                member_id=member_id,reg_state=1)
        except:
            return JSONResponse(1007)
        pag = mypage(request.GET.get('page'), obj)
        serializer = Activity_my(pag['page_list'], many=True)
        return JSONResponse(0,serializer.data,pag['page_sum'])
    else:
        return JSONResponse(1054)



@csrf_exempt
def Activity_signup(request):
    '''
    报名指定活动/修改报名状态
    :param request:
    :return:
    '''
    if request.method == 'POST':
        data = json.loads(request.body)
        try:
            member_id = QyMember.objects.get(openid=data.get('openid')).id
            is_signup = QyActor_activities.objects.get(activity_id=data.get('activity_id'), member_id=member_id)
            # 判断是否有报名参与过指定的活动（没参与过创建参与）
            if not is_signup:
                obj = QyActor_activities(
                    activity_id=data.get('activity_id'),
                    member_id = member_id,
                    reg_state =data.get('reg_state'),
                    add_time = str(time.time())
                )
                obj.save()
            # 判断参与的活动自己所处状态是否和本次修改一致（不一致进行更改）
            elif not data.get('reg_state') == is_signup.reg_state:
                is_signup.reg_state = data.get('reg_state')
                is_signup.save()
            else:
                return JSONResponse(1001)
        except:
            return JSONResponse(1007)
        objs = QyMember.objects.filter(pk=member_id)
        serializer = MemberSerializer(objs, many=True)
        return JSONResponse(0,serializer.data)
    else:
        return JSONResponse(1057)



@csrf_exempt
def Activity_delete(request):
    '''
    取消报名指定活动
    :param request:
    :return:
    '''
    if request.method == 'DELETE':
        data = json.loads(request.body)
        try:
            member_id = QyMember.objects.get(openid=data.get('openid')).id
            is_signup = QyActor_activities.objects.get(activity_id=data.get('activity_id'), member_id=member_id)
            is_signup.delete()
        except:
            return JSONResponse(1007)
        return JSONResponse(0)
    else:
        return JSONResponse(1056)



@csrf_exempt
def recommend_activity(request):
    '''
    推荐的活动列表
    :param request:
    :return:
    '''
    if request.method == 'GET':
        my_id = request.GET.get('member')
        try:
            # 查询 所有不是本人举办的活动
            objs_notOriginator = QyActivity.objects.exclude(originator_id=my_id)

            notOriginator = []
            for x in objs_notOriginator:
                notOriginator.append(x.id)

            # 获取其中 本人没有参与的活动
            notActor = []
            for y in notOriginator:
                actor_activity = QyActor_activities.objects.filter(activity_id=y, member_id=my_id)
                if not actor_activity:
                    notActor.append(y)

            # 获取其中 活动状态为可参加的
            objs = QyActivity.objects.filter(pk__in=notActor, activity_state=1)

            # 判断活动类别是 散踢(可见)/队内(与origin为好友可见)
            objs_actor = []
            for obj in objs:
                if obj.type == 0:
                    objs_actor.append(obj)
                elif obj.type == 1:
                    friend = QyGroup.objects.filter(member_a_id=obj.originator_id, member_b_id=my_id)
                    if friend:
                        objs_actor.append(obj)
                    else:
                        pass
                else:
                    pass

            serializer = Activity_info(objs_actor, many=True)
            return JSONResponse(0,serializer.data)
        except:
            return JSONResponse(1007)
    else:
        return JSONResponse(1054)